home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
bastutor.arc
/
A4.TXT
< prev
next >
Wrap
Text File
|
1983-08-01
|
12KB
|
251 lines
-----
REM Statement
All right! You have successfully completed three lessons in BASIC. By
now you should be fairly confident with the language.
The |REM~ statement allows you to insert explanatory |rem~arks into the
program. These remarks are |ignored~ by the computer when it executes the
program, but provide the programmer with information about the program. They
appear only when you LIST the program. The REM statement can begin with either
the letters |REM~ or an apostrophe (|'~) followed by the remark itself.
Here is an example of REM statements in a program.
10 DATA 45,24,97,65,47,15,80,38,77,999
20 READ X:IF X=999 THEN 70 |'If end of data goto line 60.~
30 IF X>=50 THEN LET G=G+1 |'Count numbers G.T.E. to 50.~
40 IF X<50 THEN LET L=L+1 |'Count number L.T. 50.~
50 GOTO 20 |'Get the next number.~
60 PRINT "Of the numbers,";L;"were less than 50 and";
70 PRINT G;"were greater than or equal to 50."
Try typing in and |RUN~ning this program.
-----
STOP and END Statements
There are four ways to stop the execution of a program in BASIC.
1. The program automatically stops when it runs out of lines to
execute.
2. |STOP~ Statement. When the computer comes to a STOP statement, it
prints |Break in N~ (where |N~ is the |line number~ of the STOP
statement) and stops.
3. |END~ Statement. This is similar to the STOP statement except that
the computer does not print the Break message.
4. Pressing the |Ctrl~ key and the |Break~ key (Scroll lock) together
stops the program during execution and prints a |Break in N~
message. If you want to continue with the program, simply type
|CONT~ (short for |cont~inue) and press the return key. If you haven't
edited the program, the computer will continue with the program
right where it was interrupted (this works if you used a STOP
statement as well, but not if you used an END statement).
-----
STOP and END Statements (continued)
Try out these statements and commands by typing in and |RUN~ning the
following program.
|10 PRINT "Type CONT and press return."~
|20 STOP~
|30 PRINT "GOOD! You continued the program."~
|40 PRINT "Use the Ctrl and Break keys any time now!":GOTO 40~
-----
EDIT Command
When you make (or discover) a mistake in a program line, you can
correct it by simply typing the line over. However, it is much faster to use
the BASIC Program Editor. To do this, simply type |EDIT N~ (where |N~ is the line
number to edit) and then move to the mistake using the cursor control keys, and
correct it by typing over, inserting or deleting.
If you want to completely erase that line of the program, you can do so
by typing the line number itself and pressing return. If there are several
lines you want to delete, type |DELETE N1-N2~ (where |N1~ is the first line and |N2~
is the last line to delete) and press return. This will delete all lines
between and including |N1~ and |N2~.
-----
AUTO Command
When you are first writing a program, you spend a good deal of time
just typing in the line numbers. The |AUTO~ command will take care of that for
you. Type the word AUTO and the computer will put the line numbers in
automatically.
Here is the correct form, or syntax, for the AUTO command.
AUTO [|B~[,|I~]]
|B~ is the line number at which the AUTO command will begin. If you do
not give a value for |B~ the computer will |default~ to (automatically start
at) line number 10.
|I~ is the increment value. It is the value that will be added to each
line number to get the next line number. If you do not enter a number for |I~,
it will default to 10.
-----
AUTO Command (continued)
If you want to start at some line number other than 10, say 200, type
AUTO 200 and your line numbers will begin at 200 and increase in increments of
10. If you want to start at 100 and increase by 5's then type AUTO 100,5.
When you finish typing in your program, shut off the AUTO command by pressing
the |Ctrl~ key and the |Break~ key together.
Try typing in the following program using the AUTO command.
|100 INPUT "What is the first number";A~
|105 INPUT "What is the second number";B~
|110 PRINT A;"+";"B";"=";A+B~
|115 PRINT A;"-";"B";"=";A-B~
|120 PRINT A;"*";"B";"=";A*B~
-----
TRON and TROFF Commands
If a program doesn't work properly when you try running it, it is said
to have a |bug~ in it. Much of a programmer's time is spent |de-bugging~ his
or her programs. A feature that sometimes makes this easier is the |TRON~
command. TRON is short for program |TR~acer |ON~. When you type TRON and then RUN
your program, the computer will print each line number, in brackets (|[]~) as
it is executed. Other program output will also be printed, but only the
executed line numbers will be printed in brackets.
For this program: |10 DATA 17,23,99~
|20 READ NUM~
|30 IF NUM<>99 THEN PRINT NUM:GOTO 20~
|40 PRINT "The END."~
The output using TRON
would look like this: |[10][20][30] 17~
|[20][30] 23~
|[20][30][40]The END.~
When you have found your bug and want to run your program without the
line numbers being displayed, type |TROFF~ which is short for program |TR~acer |OFF~.
This returns the computer to normal program execution.
-----
TRON and TROFF Commands (continued)
The following program is supposed to count and print the values in a
DATA statement, but for some reason it gets stuck on the first value and goes
crazy. By using the TRON and TROFF commands, try and find the bug in this
program.
|10 DATA 34,67,12,9,65,38,84,999~
|20 READ NUM:IF NUM=999 THEN GOTO 60~
|30 COUNT=COUNT+1~
|40 PRINT "Value number";COUNT;"is";NUM~
|50 GOTO 30~
|60 PRINT "There are";COUNT;"numbers in the data statement."~
First, type in the program and try to |RUN~ it. After a few seconds
press the |Ctrl~ and |Break~ keys to stop the program. Then type |TRON~ to turn the
tracer on. Again |RUN~ the program and when you have identified the problem,
stop the program and fix the bug. Now turn the tracer off by typing |TRON~ and
|RUN~ the program a third time.
-----
TRON and TROFF Commands (continued)
Here is the same program from the last page. Hopefully you found the
bug in line |50~.
10 DATA 34,67,12,9,65,38,84,999
20 READ NUM:IF NUM=999 THEN GOTO 60
30 COUNT=COUNT+1
40 PRINT "Value number";COUNT;"is";NUM
|50 GOTO 30~
60 PRINT "There are";COUNT;"numbers in the data statement."
The line should have read |50 GOTO 20~. As it was, the program never
read a new value for the variable |NUM~, so it never got to |999~ to signal the
|end of DATA~.
If you found the bug, congratulations! If not make sure you understand
how the TRON and TROFF commands work and better luck next time.
-----
RND Function
The |RND~ function returns a random number between 0 and 1. If you
want a random number between 0 and 1000, then just multiply by 1000.
example- LET |X=RND~ [this returns |X~ such that |0~ < |X~ <| 1~]
LET |Y=RND*100~ [this returns |Y~ such that |0 ~< |Y~ <| 100~]
Here is a sample program that lets you input an |upper bound~ number,
and the computer prints a list of random numbers between 0 and your upper
bound. You will have to use the |Ctrl~ and |Break~ keys to stop this program.
|10 INPUT "Enter a number to be the upper bound.",MAX~
|20 PRINT RND*MAX~
|30 GOTO 20~
-----
INT Function
The |INT~ function returns the largest integer less than or equal to
the number in parentheses.
example- |LET A=INT(9.2)~ [in this statement, |A~ will equal |9~]
|LET K=INT(-7.6)~ [in this statement, |K~ will equal |-8~]
Here is a program that uses the RND and INT functions to quiz you on
the INT function. Once again, press the |Ctrl~ and |Break~ keys when you have
had enough.
|10 NUMBER=RND*200-100~ 'pick a random number between -100 and 100
|20 PRINT "What is the INT of";NUMBER;:INPUT ANSWER~
|30 IF ANSWER<>INT(NUMBER) THEN PRINT "The answer is";INT(NUMBER)~
|40 GOTO 10~
-----
ABS Function
The |ABS~ function returns the |absolute value~ of an expression. The
absolute value is just the positive value of a number.
example- |LET X=ABS(-5)~ [in this statement, |X~ will equal |5~]
|LET C=ABS(12*4)~ [in this statement, |C~ will equal |48~]
Here is a program that uses the RND, INT and ABS functions to quiz you
on the ABS function. Once again, press the |Ctrl~ and |Break~ keys when you
have had enough.
|10 NUMBER=INT(RND*200-100)~ 'pick integer between -100 and 100
|20 PRINT "What is the ABS of";NUMBER;:INPUT ANSWER~
|30 IF ANSWER<>ABS(NUMBER) THEN PRINT "The answer is";ABS(NUMBER)~
|40 GOTO 10~
-----
ABS and INT functions (continued)
Here is another short program that you can try out. It uses the ABS
and INT functions to find the |G~reatest |C~ommon |D~enominator of two numbers.
|10 INPUT "Enter the first number.",NUM1~
|20 INPUT "Enter the second number.",NUM2~
|30 NUM1=ABS(NUM1):NUM2=ABS(NUM2)~
|40 LET R=NUM1-NUM2*INT(NUM1/NUM2)~
|50 IF R=0 THEN GOTO 70~
|60 NUM1=NUM2:NUM2=R:GOTO 40~
|70 PRINT "The greatest common denominator is";NUM2~
-----
End of Lesson Four
Congratulations on completing the |Beginning BASIC~ Course. Now to bring
together much of what you've learned, try writing the following programs.
1. A program that will find the average and the sum of a series of
numbers in a DATA statement.
2. A number guessing game program. (|Hint~: Have the computer pick a
number between 1 and 1000 and then INPUT guesses as to what the
number is.)
If you have trouble, type |LOAD"AVERAGE"~ for the average program,
|LOAD"GUESS_1"~ for one guessing game, or
|LOAD"GUESS_2"~ for a different version of the game.
and then |LIST~ to give a listing of the sample program. Type |NEW~ to erase the
sample program and start one of your own.
If you would like information on how to obtain a |The PC-Prof.~
copy of "Intermediate BASIC", volume two of the BASIC Prof. |P.O. Box 26~
series, send your name and address to: |Salina, Ks.~
|67402-0026~
-----